<?php
namespace File\Controller;
use Think\Controller;
use OSS\OssClient;
use OSS\Core\OssException;

require_once "./ThinkPHP/Library/Vendor/OSS/autoload.php";

class FileController extends Controller {

    //头像及文件上传接口
    public function fileupload() {
        $token=I('token');
//        $ftype=I('image');
//        //这里划分一下允许上传的文件类型，与3.1相比，文件类型不再是数组类型了，而是字符串，这是个区别。
//        if ($ftype == 'image') {
//            $ftype = 'jpg,gif,png,jpeg,bmp';
//        } else if ($ftype == 'file') {
//            $ftype = 'zip,rar,doc,xls,ppt';
//        }
        $info=M('user')->where(array('token'=>$token))->field('phonenumber')->select();

        if($info[0]['phonenumber']=='')
        {
            $this->error('您访问的页面不存在');
            return;
        }
        $setting = array (
            'maxSize' => 5 * 1024 * 1024,//文件大小
            'rootPath' => 'http://image.chinaola.net/',
            'saveName' =>md5($info[0]['phonenumber']) ,
            'savePath' => 'aliyun/headimage/',    //保存路径
            'driver' => 'Aliyun',
            'driverConfig' => array (
                'AccessKeyId' => 'R13sMXDDTfh4RjSL',    //AccessKeyId
                'AccessKeySecret' => 'GMqYWqWPv0AuJCxCSbTrPHKpk7F3zt',//AccessKeySecret
                'domain' => OSS,        //
                'Bucket' => 'ola',         //Bucket
                'Content-Type' => 'image/jpeg',
                'Endpoint' => 'http://oss-cn-shenzhen.aliyuncs.com',
            ),
        );
//
        $config = array (
            'AccessKeyId' => 'R13sMXDDTfh4RjSL',    //AccessKeyId
            'AccessKeySecret' => 'GMqYWqWPv0AuJCxCSbTrPHKpk7F3zt',//AccessKeySecret
            'domain' => OSS,        //
            'Bucket' => 'ola',         //Bucket
            'Endpoint' => 'http://oss-cn-shenzhen.aliyuncs.com',
            'saveName'=>md5($info[0]['phonenumber']),
        );

        try {
            $ossClient = new OssClient($config['AccessKeyId'],$config['AccessKeySecret'],$config['Endpoint']);

        } catch (OssException $e) {

        }

        $hc_big=explode('.',$_FILES['image']['name']);
        $big_aaa=$hc_big['1'];//获取图片后缀
        try{
             $ossClient->uploadFile($config['Bucket'],$setting['savePath'].$setting['saveName'].'/' .$setting['saveName'].'.'.$big_aaa, $_FILES["image"]["tmp_name"]);
        } catch(OssException $e) {
            $result=array(
                'code'=>'0',
                'message'=>'上传失败'
            );
            json_encode($result,JSON_UNESCAPED_UNICODE);
           return;
        }
        $filepath= $setting['rootPath']. $setting['savePath'].$setting['saveName'].'/'.$setting['saveName'].'.'.$big_aaa;
        $image=array(
            'headurl'=>$filepath,
        );
        M('userdetail')->where(array('token'=>$token))->save($image);//头像路径保存到数据库表格中
        $result=array(
            'code'=>'1',
            'message'=>'上传成功',
            'url'=>$filepath,
        );
        echo json_encode($filepath);
    }


    public function index(){
        $this->display();
    }



}
